<?php
namespace app\common\service;

use app\admin\model\kuerp\Customerlevel;
use app\admin\model\kuerp\Customers;
use app\admin\model\kuerp\Customertype;
use think\Db;

class CustomerService
{

    public static function add($appUserInfo){

        $customerType = Customertype::where('status', 1)->find();
        $customerLevel = Customerlevel::where('status', 1)->find();

        $result = Customers::create([
            'customer_code' =>  self::getCodes('',1),
            'customertype_id' => $customerType ? $customerType['id'] : 0,
            'customerlevel_id' => 1,
            'customer_name' => $appUserInfo['nickname'],
            // 'saler' => '',
            'connet_name' => $appUserInfo['nickname'],
            'customer_mobile' => $appUserInfo['mobile'] ?? '',
            'customer_phone' => $appUserInfo['mobile'] ?? '',
            'qq_wx_email' => $appUserInfo['email'] ?? '',

        ]);

        // 追加客户联系额外信息
        if($result){
            Db::name('kuerp_customers_user')->insert([
                'customers_code' => $result['customer_code'],
                'sid' => $result['id'],
                'createtime' => time(),
                'first_connect_name' => $appUserInfo['nickname'],
                'first_connect_mobile' => $appUserInfo['mobile'] ?? '',
                'first_connect_phone' => $appUserInfo['mobile'] ?? '',
                'qq_wx_email' => $appUserInfo['email'] ?? '',
                'connect_address' => '',
            ]);
        }

        return $result;

    }

    /**
     * 获取编号
     * @param int $type   编号类型
     * @return mixed
     */
    public static function getCodes($code,$type,$new=true)
    {
        $set = \think\Db::name('kuerp_prefix')->where(['type'=>$type])->find();
        if(empty($set)){
            return '';
        }else{
            if(!empty($set['dbname']) && !empty($set['field']) && empty($code) && $new && $type<7){
                $code = \think\Db::name($set['dbname'])->order([$set['field']=>'DESC'])->value($set['field']);
            }
            preg_match_all("/\d+/",$code,$num);
            $order_num = implode('',$num[0]);
            if($code && $new){
                try{
                    if($set['year']==1){
                        $order_num = substr($order_num, 4);
                    }
                    if($set['month']==1){
                        $order_num = substr($order_num, 2);
                    }
                    if($set['day']==1){
                        $order_num = substr($order_num, 2);
                    }
                    if($set['hour']==1){
                        $order_num = substr($order_num, 2);
                    }
                    if($set['minute']==1){
                        $order_num = substr($order_num, 2);
                    }
                    if($set['second']==1){
                        $order_num = substr($order_num, 2);
                    }
                }catch (\Exception $e){}
            }
            if($order_num<$set['start']){
                $order_num = $set['start'];
            }else{
                $order_num = (float)$order_num+1;
            }
            $prefix = $set['prefix'];
            if($set['year']==1){
                $prefix = $prefix.date('Y');
            }
            if($set['month']==1){
                $prefix = $prefix.date('m');
            }
            if($set['day']==1){
                $prefix = $prefix.date('d');
            }
            if($set['hour']==1){
                $prefix = $prefix.date('H');
            }
            if($set['minute']==1){
                $prefix = $prefix.date('i');
            }
            if($set['second']==1){
                $prefix = $prefix.date('s');
            }
            $s = '';
            if(strlen($order_num)<$set['strlen']){
                for ($a=1;$a<=$set['strlen']-(strlen($order_num));$a++){
                    $s .= '0';
                }
            }
            $code = $order_num;
            $order_num = $prefix.$s.$order_num;
        }
       
        return $order_num;
    }
    
}
